package com.hyt.it.ogt.kq.common.gov.utils;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

import com.hyt.it.ogt.kq.common.gov.constant.ArrangeParamCodeEnum;

import cn.hutool.core.lang.PatternPool;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 *
 * </p>
 *
 * @author Chengxh
 * @since 2021-08-14
 */
@Slf4j
public class StringUtil {

    public final static String REGEX_START = "^[　*| *| *|//s*]*";
    public final static String REGEX_END = "[　*| *| *|//s*]*$";
    public final static String REPLACE = "";
    public final static String SOURCE = "0123456789";
    public final static String SOURCE2 = "123456789";
    private static Random rand = new Random();

    /**
     * 去除字符串中 头部 和 尾部 所包含的空格
     * 包括:空格(全角，半角)、制表符、换页符等
     * @param s
     * @return
     */
    public static String trims(String s) {
        if(s == null){
            return null;
        }
        return s.replaceAll(REGEX_START, REPLACE).replaceAll(REGEX_END, REPLACE);
    }

    /**
     * 获取随机数
     * @param index
     * @return
     */
    public static String getRandom(int index){
        StringBuffer flag = new StringBuffer();
        for (int j = 0; j < index; j++) {
            flag.append(SOURCE.charAt(rand.nextInt(9)));
        }
        return flag.toString();
    }

    public static void main(String[] args) {
        Integer i = 1;
        inn(i);
        System.out.println(i);
    }

    private static void inn(Integer i) {
        i += 10;
    }

    /**
     * 获取随机数<num内的随机数
     * @param num
     * @return
     */
    public static Integer getRandomInteger(int num){
        return new Random().nextInt(num) + 1;
    }

    /**
     * 获取随机数（>0）
     * @param index
     * @return
     */
    public static Integer getRandomNumber(int index){
        StringBuffer flag = new StringBuffer();
        for (int j = 0; j < index; j++) {
            if(j == 0) {
                flag.append(SOURCE2.charAt(rand.nextInt(9)));
            }else{
                flag.append(SOURCE.charAt(rand.nextInt(9)));
            }
        }
        return Integer.valueOf(flag.toString());
    }

    /**
     * 截取字符串后几位
     * @param str 字符串
     * @param figures 后几位
     * @return
     */
    public static String stringRight(String str,int figures) {
        int len = str.length();
        if (figures > len) {
            return str;
        }
        return str.substring(len - figures,len);
    }

    /**
     * 不足位数，补零
     * @param number
     * @param figures 位数
     * @return
     */
    public static String stringFormatZero(int number,int figures) {
        String format = StringUtils.join("%0",figures,"d");
        return String.format(format,number);
    }

    /**
     * 获取字母和数字的随机数
     * @param digit 位数
     * @param type 类型：0：数字，1：字母 其他：字母和数字
     * @return
     */
    @Deprecated
    public static String randomStringOrNumber2(int digit,int type) {
        String baseLetterUp = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String baseLetterLow = "abcdefghijklmnopqrstuvwxyz";
        String baseLetterNu = "0123456789";
        String[] baseRandom = {baseLetterNu,baseLetterUp,baseLetterLow};
        String base = "";

        if(type == ArrangeParamCodeEnum.RANDOM_TYPE_NUMBER.getCode().intValue()) {
            //只勾选了数字
            base = StringUtils.join(baseLetterNu);
        } else  if(type == ArrangeParamCodeEnum.RANDOM_TYPE_LETTER.getCode().intValue()) {
            //只勾选了字母
            base = StringUtils.join(baseLetterUp);
        } else  if(type == ArrangeParamCodeEnum.RANDOM_TYPE_LETTER_LOW.getCode().intValue()) {
            //只勾选了字母
            base = StringUtils.join(baseLetterLow);
        } else {
            base = StringUtils.join(baseLetterUp,baseLetterNu,baseLetterLow);
        }

        StringBuilder builder = new StringBuilder();
        for (int i=0 ; i<digit ; i++) {
            int number = rand.nextInt(base.length());
            builder.append(base.charAt(number));
        }

        return builder.toString();
    }

    /**
     * 随机数
     * @param digit
     * @return
     */
    public static String randomNumber(int digit) {
        String baseNumber = "0123456789";
        StringBuilder builder = new StringBuilder();
        for (int i=0 ; i<digit ; i++) {
            int number = rand.nextInt(baseNumber.length());
            builder.append(baseNumber.charAt(number));
        }

        if (builder.length() < digit) {
            return stringFormatZero(Integer.parseInt(builder.toString()),digit);
        }

        return builder.toString();
    }

    public static String randomStringOrNumberNew(int digit, String randomParam) {
        List<String> randomParamList = Arrays.asList(randomParam.split(","));
        if (randomParamList.contains(String.valueOf(ArrangeParamCodeEnum.BASE_NUMBER.getCode()))
                && !randomParamList.contains(String.valueOf(ArrangeParamCodeEnum.BASE_CHAR_UP.getCode()))) {
            //仅包含数字
            return RandomUtil.randomNumbers(digit);
        } else if (!randomParamList.contains(String.valueOf(ArrangeParamCodeEnum.BASE_NUMBER.getCode()))
                && randomParamList.contains(String.valueOf(ArrangeParamCodeEnum.BASE_CHAR_UP.getCode()))) {
            //仅包含大写字母
            return RandomUtil.randomString(RandomUtil.BASE_CHAR, digit).toUpperCase();
        } else if (randomParamList.contains(String.valueOf(ArrangeParamCodeEnum.BASE_NUMBER.getCode()))
                && randomParamList.contains(String.valueOf(ArrangeParamCodeEnum.BASE_CHAR_UP.getCode()))) {
            //既包含数字，也包含大写字母
            return RandomUtil.randomStringUpper(digit);
        }
        return null;
//        String baseLetterNu = "0123456789";
//        String baseLetterUp = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//        //String baseLetterLow = "abcdefghijklmnopqrstuvwxyz";
//        String baseLetterAll = StringUtils.join(baseLetterNu, baseLetterUp);
//        String[] baseRandom = {baseLetterNu, baseLetterUp, baseLetterAll};
//
//        String[] randomParams = randomParam.split(",");
//        StringBuilder baseBuffer = new StringBuilder();
//        for (String param : randomParams) {
//            int type = Integer.parseInt(param);
//            baseBuffer.append(baseRandom[type]);
//        }
//
//        StringBuilder builder = new StringBuilder();
//        Random random = new Random();
//        for (int i = 0; i < digit; i++) {
//            int number = random.nextInt(baseBuffer.toString().length());
//            builder.append(baseBuffer.toString().charAt(number));
//        }
//
//        return builder.toString();
    }

    public static String getUrl(HttpServletRequest request) {
        return request.getRequestURL().toString();
    }

    public static String getHost(String url) throws MalformedURLException {
        URL u = new URL(url);
        return u.getHost();
    }

    /**
     * 逗号分割的字符串转为List<String>
     * @param str 逗号分割的字符串
     * @return List<String>
     */
    public static List<String> stringToList(String str) {
        if (StringUtils.isEmpty(str)) {
            return null;
        }
        return Arrays.stream(str.split(","))
                .map(String::trim)
                .collect(Collectors.toList());
    }

    /**
     * 逗号分割的字符串转为字符串数组
     * @param str 逗号分隔的字符串
     * @return 字符串数组
     */
    public static String[] stringToArray(String str) {
        if (StringUtils.isEmpty(str)) {
            return null;
        }
        return str.split(",");
    }

    /**
     * 是否为手机号
     * @param value
     * @return
     */
    public static boolean isMobile(CharSequence value) {
        return BooleanUtil.orOfWrap(
                //大陆手机
                Validator.isMatchRegex(PatternPool.MOBILE, value),
                //香港手机
                Validator.isMatchRegex(PatternPool.MOBILE_HK, value),
                //澳门手机
                Validator.isMatchRegex(PatternPool.MOBILE_MO, value),
                //台湾手机
                Validator.isMatchRegex(PatternPool.MOBILE_TW, value));
    }

}
